var bgAudioManager;
var innerAudioContext;

function loadbgaudio() {
	// bgAudioManager = uni.getBackgroundAudioManager();
	innerAudioContext = uni.createInnerAudioContext();
}

function navTo(path, i) {
	let url = i && i.id ? `/pages/${path}?id=${i.id}` : `/pages/${path}`
	uni.navigateTo({
		url
	})
}

function switchTab(path) {
	uni.switchTab({
		url: `/pages/${path}`
	})
}

function randomText() {
	//生成6位i随机数
	var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	var arr = str.split("");
	var result = "";
	for (var i = 0; i < 6; i++) {
		var n = Math.floor(Math.random() * arr.length);
		result += arr[n];
	}
	return result
}

function toast(value, duration, icon) {
	uni.showToast({
		title: value,
		duration: duration ? duration : 1200,
		icon: icon ? icon : "none",
	})
}

function randomNum(m, n) {
	var aNumber = (n - m) * Math.random() + m + 1;
	var result = Math.floor(aNumber);
	return result;
}

function secondsForamt(value) {
	let theTime = value; //秒
	let middle = 0; //分
	let hour = 0; //小时
	if (theTime > 59) {
		middle = parseInt(theTime / 60);
		theTime = parseInt(theTime % 60);
	}
	if (middle > 59) {
		hour = parseInt(middle / 60);
		middle = parseInt(middle % 60);
	}
	theTime < 10 ? theTime = '0' + theTime : theTime = theTime
	middle < 10 ? middle = '0' + middle : middle = middle
	hour < 10 ? hour = '0' + hour : hour = hour
	return hour + ':' + middle + ':' + theTime
}

function clearAudio() {
	innerAudioContext.pause()
	// bgAudioManager.pause()
	innerAudioContext.src = ''
	// bgAudioManager.src = ''
}

//时间戳转换方法    date:时间戳数字
function formatDate(date) {
	date = date + ''
	if (date.length == 10) {
		date = date * 1000
	}
	var date = new Date(date);
	var YY = date.getFullYear() + '-';
	var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
	var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
	var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
	var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
	return YY + MM + DD + " " + hh + mm + ss;
}

function setA(src) {
	if (src) {
		innerAudioContext.src = src;
		innerAudioContext.play()
	} else {
		console.log(222);
		innerAudioContext.src = ''
		innerAudioContext.pause()
	}
}

function getArrDifference(arr1, arr2) {
	return arr1.concat(arr2).filter((v, i, arr) => {
		return arr.indexOf(v) === arr.lastIndexOf(v);
	})
}

export default {
	navTo,
	switchTab,
	toast,
	setA,
	clearAudio,
	secondsForamt,
	loadbgaudio,
	randomNum,
	formatDate,
	randomText,
	getArrDifference
}